﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DbFrame.Test
{
    using DbFrame.SqlServer;
    using DbFrame.Test.Class;
    using Models;

    class Program
    {
        static void Main(string[] args)
        {
            Hzy<Member, Sys_User> _Hzy = new Hzy<Member, Sys_User>();

            _Hzy.Join((tabs) => tabs.Item1.Member_UserID == tabs.Item2.User_ID);

            _Hzy.Query((item) => item.Item1.Member_UserID);

            DbContextSqlServer.SetDefaultConnectionString("Server=.;Database=AppDB;User ID=sa;Password=123456;");

            var db = new DbContextSqlServer();

            var x = new { user = "hzy", age = "24" };

            var str = "1,1,1,2".Replace("1", "2");

            Type t = x.GetType();

            var guids = new List<Guid>() { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };

            var _Iquery = db.Query<Member>()
                .Where(item => item.Member_ID.In(guids))// 语法 IN(1,2,3)
                .OrderByDesc(item => item.Member_CreateTime);
            //.OrderBy(item => item.Member_CreateTime);
            //.OrderBy(member => new { desc = member.Member_CreateTime });//.Top(1).ToList();

            //分页
            var _Meber_List = _Iquery.TakePage(1, 20).ToList<dynamic>();

            //总数
            //var _Count = _Iquery.Count();

            //var _Max = _Iquery.Max(w => w.Member_CreateTime);

            //var _Min = _Iquery.Min(w => w.Member_CreateTime);

            //var _Sum = _Iquery.Sum(w => w.Member_Phone);

            var _Member = db.Find<Member>(w => w.Member_ID == "9a604aa2-9ae6-4a2f-8ddb-d9e0289ead9e".ToGuid());

            var _Sql = db.Query<Member>()
                         .Join<Sys_User>((a, b) => a.Member_UserID == b.User_ID)
                         .Where((a, b) => a.Member_Sex == "男")
                         .Select((a, b) => a)//new { a.Member_Name, b.User_LoginName })
                         .ToList<dynamic>();

            var _Member_Frist = db.Query<Member>()
                          .Join<Sys_User>((a, b) => a.Member_UserID == b.User_ID)
                          .Where((a, b) => a.Member_Sex == "男")
                          .Select((a, b) => new { a, b.User_LoginName })
                          .Frist<dynamic>();

            var _Member_Frist_0 = db.Query<Member>()
                      .Join<Sys_User>((a, b) => a.Member_UserID == b.User_ID)
                      .Where((a, b) => a.Member_Sex == "男")
                      .Select((a, b) => a.Member_Name)
                      .Frist<dynamic>();

            var filed = "Member_Name";

            var _Member_Frist_1 = db.Query<Member>()
                         .Join<Sys_User>((a, b) => a.Member_UserID == b.User_ID)
                         .Where((a, b) => a.Member_Sex == "男")
                         .Select((a, b) => filed)
                         .Frist<dynamic>();

            foreach (var item in _Sql)
            {
                var a = item.Member_Name;
            }

            db.Commit(() =>
            {


                /**int  自增**/
                //var _int_tab = new int_tab();
                //_int_tab.name = "hzy" + DateTime.Now;

                //_int_tab.id = db.Insert<int_tab>(_int_tab).ToInt32();

                /*** guid 主键 ***/

                for (int i = 0; i < 100; i++)
                {
                    _Member = new Member();
                    _Member.Member_Birthday = DateTime.Now;
                    _Member.Member_Introduce = "111";
                    _Member.Member_Name = "wjf" + DateTime.Now;
                    _Member.Member_Num = "1";
                    _Member.Member_Phone = 123123123;
                    _Member.Member_Sex = "男";
                    _Member.Member_ID = db.Insert<Member>(_Member).ToGuid();

                }

                var iquery = db.Query<Member>()
                      .Where(a => a.Member_Sex.Contains("男"))
                      .Select(a => new { a.Member_Name })
                      .ToList<dynamic>();

                var list = db.QueryBySql<Member>("select * from Member with(nolock)");

                throw new Exception("123");
            });

            //db.DeleteById<Member>(_Member.Member_ID);





        }
    }
}
